package net.nagaseyasuhito.kinsenka.wicket.page;

import net.nagaseyasuhito.kinsenka.persist.dao.UserDao;
import net.nagaseyasuhito.kinsenka.persist.entity.User;
import net.nagaseyasuhito.kinsenka.wicket.KinsenkaSession;
import net.nagaseyasuhito.kinsenka.wicket.component.IdentifierField;

import org.apache.wicket.Application;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;

import com.google.inject.Inject;

public class SignUpPage extends BasePage {
	private static final long serialVersionUID = 1L;

	private User user;

	@Inject
	private UserDao userDao;

	public SignUpPage() {
		Form<SignUpPage> signUpForm = new StatelessForm<SignUpPage>("signUpForm", new CompoundPropertyModel<SignUpPage>(this));
		signUpForm.add(new FeedbackPanel("feedback"));
		signUpForm.add(new IdentifierField("user.signInId").setRequired(true));
		signUpForm.add(new PasswordTextField("user.password"));
		signUpForm.add(new PasswordTextField("confirmPassword", new Model<String>()));

		signUpForm.add(new EqualPasswordInputValidator((FormComponent<?>) signUpForm.get("user.password"), (FormComponent<?>) signUpForm.get("confirmPassword")));

		signUpForm.add(new Button("signUp") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				SignUpPage.this.userDao.persist(SignUpPage.this.user);

				KinsenkaSession.get().info(this.getLocalizer().getString("signUpSuccessed", this));
				this.setResponsePage(Application.get().getHomePage());
			}
		});

		this.add(signUpForm);
	}
}
